package com.drug.mingkang.controller;

import com.drug.mingkang.mapper.ImmTrackingnumberMapper;
import com.drug.mingkang.model.AuditRecord;
import com.drug.mingkang.model.ImmPurchase;
import com.drug.mingkang.model.ImmTrackingnumber;
import com.drug.mingkang.model.User;
import com.drug.mingkang.model.vo.AuditRecordVo;
import com.drug.mingkang.model.vo.ImmPurchaseVo;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.ExperssageService;
import com.drug.mingkang.service.ImmPurchaseService;
import com.drug.mingkang.utils.AuditStatus;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.*;

/**
 * 分公司采购(ImmPurchase)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:14
 */
@Api(value = "分公司采购计划单表控制层",tags = "分公司采购计划单Contorller")
@RestController
@RequestMapping("immPurchase")
public class ImmPurchaseController {
    /**
     * 服务对象
     */
    @Resource
    private ImmPurchaseService immPurchaseService;

    @Resource
    private ResponseMessage responseMessage;

    @Resource
    private Tools tools;

    @Resource
    private ImmTrackingnumberMapper immTrackingnumberMapper;

    @Resource
    private ExperssageService experssageService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ImmPurchase selectOne(String id) {
        return this.immPurchaseService.queryById(id);
    }


    /**
     * 通过实体作为筛选条件查询
     * @param immPurchaseVo
     * @return
     */
    @ApiOperation(value = "查询分公司所有采购单",notes = "查询分公司所有采购单",httpMethod = "POST",response = Map.class)
    @PostMapping("queryAll")
    public Map<String,Object> queryAll(ImmPurchaseVo immPurchaseVo,HttpSession session){

        //从session中获取用户信息
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        //判断如果公司ID是0就设置空，否则就传公司ID
        if(perUsers.getCompanyId().equals("0")){
            immPurchaseVo.setImmId(null);
        }else {
            immPurchaseVo.setImmId(perUsers.getCompanyId());
        }

        //调用queryAll方法 查询出所有的采购信息
        List<ImmPurchase> list = immPurchaseService.queryAll(immPurchaseVo);
        //调用count方法 查询出行数
        int count = immPurchaseService.count(immPurchaseVo);
        //调用工具类limit方法 转map格式
        Map<String, Object> map = tools.limit(list,count);
        //返回map
        return map;
    }

    /**
     * 根据采购订单ID查询物流单号
     */
    @PostMapping("whetherYqs")
    public List<String> whetherYqs(ImmPurchaseVo immPurchaseVo,HttpSession session){

        //从session中获取用户信息
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");

        //判断如果公司ID是0就设置空，否则就传公司ID
        if(perUsers.getCompanyId().equals("0")){
            immPurchaseVo.setImmId(null);
        }else {
            immPurchaseVo.setImmId(perUsers.getCompanyId());
        }

        // 调用selectTrackingNumber方法 查询出该公司的所有物流单号
        List<ImmTrackingnumber> immTrackingnumbers = immPurchaseService.selectTrackingNumber(immPurchaseVo);

        //new一个新的list
        List<String> list3 = new ArrayList<>();

        //循环物流单号的集合
        for (ImmTrackingnumber immTrackingnumber : immTrackingnumbers) {
            //调用findExpressageInfo方法 查询出所有的物流信息
            Map<String, Object> yto = experssageService.findExpressageInfo("YTO",immTrackingnumber.getTrackingnumber());
            //循环物流信息的集合
            for (String s : yto.keySet()) {
                //查询物流信息中有没有已签收的字样
                int one = yto.get(s).toString().indexOf("已签收");
                //如果找到了
                if(one>-1){
                    //就加入到new的list中
                    list3.add(immTrackingnumber.getPurchaseId());
                }
            }
        }
        //返回这个list
        return list3;
    }


    /**
     * @author Mr.wang
     * 通过实体作为筛选条件查询
     * @param immPurchaseVo
     * @return map
     */
    @PostMapping("queryAll2")
    public Map<String,Object> queryAll2(ImmPurchaseVo immPurchaseVo,HttpSession session){
        String isva = immPurchaseVo.getIsva();
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        String companyId = perUsers.getCompanyId();
        System.out.println(companyId+"公司id");
        immPurchaseVo.setImmId(perUsers.getCompanyId());
//        if(!companyId.equals("0")){
//            immPurchaseVo.setImmId(perUsers.getCompanyId());
//        }
        List<ImmPurchase> list = immPurchaseService.queryAll2(immPurchaseVo);
        int count = immPurchaseService.countW(immPurchaseVo);
        Map<String, Object> map = tools.limit(list,count);
        return map;
    }


    /**
     * @author Mr.wang
     * 通过实体作为筛选条件查询
     * @param immPurchaseVo
     * @return map
     */
    @PostMapping("queryAll3")
    public Map<String,Object> queryAll3(ImmPurchaseVo immPurchaseVo,HttpSession session){
        String isva = immPurchaseVo.getIsva();
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        immPurchaseVo.setImmId(perUsers.getCompanyId());
        if(perUsers.getCompanyId().equals("0")){
            immPurchaseVo.setImmId(null);
        }
        List<ImmPurchase> list = immPurchaseService.queryAll2(immPurchaseVo);
        List<ImmPurchase> list2 = new LinkedList<>();
        for (int i = 0; i < list.size() ; i++) {
            ImmPurchase immPurchase = list.get(i);
            String purchaseId = immPurchase.getPurchaseId();
            String s = purchaseId + ",tui";
            ImmTrackingnumber immTrackingnumber = immTrackingnumberMapper.queryById(s);
            if(immTrackingnumber!=null){
                Map<String, String> zto = experssageService.findExpressageInfoTraces("YTO",immTrackingnumber.getTrackingnumber());
                //判定是否签收
                String signfor = "";
                for(Map.Entry<String, String> map1 :zto.entrySet()){
                    System.out.println(map1);
                    String value = map1.getValue();
                    int one = value.indexOf("已签收");
                    if(one!=-1){
                        signfor="已签收";
                        immPurchase.setIsva(66);
                    }else{
                        immPurchase.setIsva(1);
                    }
                }
                list2.add(immPurchase);
            }else{
                immPurchase.setIsva(1);
                list2.add(immPurchase);
            }
        }
        int count = immPurchaseService.countW(immPurchaseVo);
        Map<String, Object> map = tools.limit(list2,count);
        return map;
    }



    /**
     * @author Mr.wang
     * 查询所有的93状态的数据
     * @param immPurchaseVo
     * @return map
     */

    @PostMapping("queryAll4")
    public Map<String,Object> queryAll4(ImmPurchaseVo immPurchaseVo,HttpSession session){
        String isva = immPurchaseVo.getIsva();
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        immPurchaseVo.setImmId(null);
        if(!perUsers.getCompanyId().equals("0")){
            immPurchaseVo.setImmId(perUsers.getCompanyId());
        }
        List<ImmPurchase> list = immPurchaseService.queryAllByLimit2(immPurchaseVo);
        int count = immPurchaseService.count4(immPurchaseVo);
        Map<String, Object> map = tools.limit(list,count);
        return map;
    }



    /**
     * 修改或增加
     * @param immPurchase
     * @param arr
     * @param session
     * @return
     */
    @PostMapping("editAdd")
    public ResponseMessage editAdd(ImmPurchase immPurchase, String arr, HttpSession session){
        int rows =  immPurchaseService.insertSelective(immPurchase,arr,session);
        return rows > 0 ? ResponseMessage.success("操作成功") : ResponseMessage.error("操作失败");
    }


    /**
     * 修改审核状态
     * @param purchaseId
     * @return
     */
    @PostMapping("updateReviewState")
    public ResponseMessage updateReviewState(String purchaseId, String status){
        status = String.valueOf(AuditStatus.YES_SQ.status);
        int rows = immPurchaseService.fupdate(purchaseId,status);
        return rows > 0 ? ResponseMessage.success("操作成功") : ResponseMessage.error("操作失败");
    }


    /**
     * 采购订单审核 修改审核状态
     * @param immPurchaseVo Vo
     * @return
     */
    @PostMapping("updateIsva")
    public ResponseMessage updateIsva(ImmPurchaseVo immPurchaseVo, AuditRecord auditRecord,HttpSession session){
        return immPurchaseService.updateStatus(immPurchaseVo,auditRecord,session);
    }


    /**
     * 根据采购订单ID查询数据
     * @param purchaseId
     * @return
     */
    @GetMapping("queryByPurchaseId/{purchaseId}")
    public ImmPurchase queryByPurchaseId(@PathVariable("purchaseId")String purchaseId){
        ImmPurchase immPurchase = immPurchaseService.queryByPurchaseId(purchaseId);
        return immPurchase;
    }


    /**
     * 删除
     * @param purchaseId
     * @return
     */
    @DeleteMapping("delete/{purchaseId}")
    public ResponseMessage delete(@PathVariable("purchaseId")String purchaseId){
        int rows = immPurchaseService.updateIsva(purchaseId);
        return rows > 0 ? ResponseMessage.success("删除成功") : ResponseMessage.error("删除失败");
    }


    /**
     * 根据分公司id和状态查询
     */
    @ApiOperation(value = "根据分公司id和状态查询", notes = "输入分公司id和状态", httpMethod = "POST", response = Map.class)
    @ResponseBody
    @PostMapping("/showList")
    public Map<String, Object> showList(ImmPurchaseVo immPurchaseVo,HttpSession session){
        String isva = immPurchaseVo.getIsva();
        System.out.println(isva);
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        //判断是否为总公司人员
        if("0".equals(perUsers.getCompanyId())){
            immPurchaseVo.setImmId(null);
        }else{
            immPurchaseVo.setImmId(perUsers.getCompanyId());
        }
        return immPurchaseService.queryAllByLimit(immPurchaseVo);
    }



    /**
     * 修改审核状态
     * @param purchaseId
     * @return
     */
    @PostMapping("updateTjcwsq")
    public ResponseMessage updateTjcwsq(String purchaseId, String status){

        status = String.valueOf(AuditStatus.YES_TJCWSQ.status);
        int rows = immPurchaseService.fupdate(purchaseId,status);
        return rows > 0 ? ResponseMessage.success("操作成功") : ResponseMessage.error("操作失败");
    }

}